import { _decorator, Camera, Component, Director, director, gfx, ImageAsset, Rect, rect, RenderTexture, SpriteFrame, Texture2D, UITransform, view, } from 'cc';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
const { ccclass, property } = _decorator;
// 截屏
@ccclass('ScreenShotCmp')
export class ScreenShotCmp extends Component {
    @property(Camera)
    screenshotCamera: Camera = null;

    renderTex: RenderTexture = null;
    _bufferView: ArrayBufferView = null;
    region: gfx.BufferTextureCopy = null;
    img: ImageAsset = null;

    finishCb = null;
    rect: Rect = rect(0, 0, 0, 0);

    protected onLoad(): void {
        // 监听事件
        EventManager.on(EventTypes.CameraEvents.ScreenShot, this.onScreenShot, this);

        const _size = view.getVisibleSize();
        this.renderTex = new RenderTexture();
        this.renderTex.reset({
            width: _size.width,
            height: _size.height
        });
        this.region = new gfx.BufferTextureCopy();
        this.screenshotCamera.targetTexture = this.renderTex;
        this.screenshotCamera.node.active = false;
    }

    // Rect 矩形区域(锚点左下角)
    onScreenShot(rect, finishCb) {
        this.finishCb = finishCb;
        this.rect.set(rect);
        // 开启截屏
        console.log('# 开启截屏', rect);
        // this.screenshotCamera.targetTexture = this.renderTex;
        this.screenshotCamera.node.active = true;
        // 注册监听渲染结束事件
        director.on(Director.EVENT_AFTER_DRAW, this.onAfterDraw, this);
    }
    // 渲染结束回调
    onAfterDraw() {
        // 注销
        director.off(Director.EVENT_AFTER_DRAW, this.onAfterDraw, this);
        // 截图
        this.capture();

        this.screenshotCamera.node.active = false;
    }

    // 截图 矩形区域(锚点左下角)
    capture() {

        // 清除
        this._bufferView = null;
        // 创建
        let width = this.rect.width;
        let height = this.rect.height;
        let rt = this.renderTex;
        // let texBuffers: ArrayBufferView[] = [];
        // texBuffers[0] = new Uint8Array(width * height * 4);
        //
        // this.region.texOffset.x = Math.round(this.rect.x);
        // this.region.texOffset.y = Math.round(this.rect.y);
        // this.region.texExtent.width = width;
        // this.region.texExtent.height = height;
        // director.root.device.copyTextureToBuffers(rt.getGFXTexture(), texBuffers, [this.region]);
        // this._bufferView = texBuffers[0];
        //
        this._bufferView = rt.readPixels(this.rect.x, this.rect.y, width, height);
        //
        this.showImage(width, height);
        // this.screenshotCamera.targetTexture = null;
    }

    showImage(width: number, height: number): SpriteFrame {
        if (this.img) {
            //回收内部数据
            this.img.destroy();
            this.img = null;
        }
        this.img = new ImageAsset();
        this.img.reset({
            _data: this._bufferView,
            width: width,
            height: height,
            format: Texture2D.PixelFormat.RGBA8888,
            _compressed: false,
        });

        let texture = new Texture2D();
        texture.reset({
            width: width,
            height: height,
            format: Texture2D.PixelFormat.RGBA8888,
            mipmapLevel: 0
        });

        let sf = new SpriteFrame();
        texture.image = this.img;
        sf.texture = texture;
        sf.packable = false; // 默认不合批
        sf.flipUVY = true;   // 翻转图片

        console.log('# 截屏成功');

        this.finishCb && this.finishCb(sf, this._bufferView);
        this.finishCb = null;

        return sf;
    }

}

